// pages/homepage.js
const app = getApp()
Page({
  onShareAppMessage() {
    return {
      title: 'swiper',
      path: 'page/component/pages/swiper/swiper'
    }
  },
  /**
   * 页面的初始数据
   */
  data: {
    disabled: false,
    isProcessing: false,
    param: {},
    showToastTitle: '',
    showTip: false,
    showBtn: true,
    navigationSetting: {
      title: '订单详情',
      deliveryPointName:'',
      navigationBarTextStyle: "white",
      height: app.globalData.navBarHeight,
      paddingTop: app.globalData.statusBarHeight,
      backgroundColor: '#c8102e',
      size: 'default',
      back: 'true',
      expireTime: '',
      status: ''
    },
    goodsTypeResponse: {
      goodsWeight: 1
    },

    isShowOpacity: false,
    goodTypeData: [],
    showType: 'goods',
    fromWhere: '1',
    isShowModal: false,
    formData: {
      contactName: '',
      phoneNum: '',
      price: 0,
      takeoffConnId: null,
      landingConnId: null,
    },
    isSuccessSave: false,
    isEndSuccessSave: false,
    ingTasksNum: 0,
    background: ['demo-text-1', 'demo-text-2', 'demo-text-3'],
    indicatorDots: false,
    autoplay: true,
    interval: 5000,
    duration: 500,
    focusTasks: []
  },
  confirmSubmit(e) {
    let formData = this.data.formData
    formData.goodsName = e.detail.goodsTypeName
    formData.code = e.detail.code
    formData.goodsTypeId = e.detail.goodsTypeId
    formData.id = e.detail.id
    formData.goodsWeight = e.detail.goodsWeight
    this.setData({
      'isShowModal': false,
      isShowOpacity: false,
      formData
    })
  },
  submitOrder() {
    if (this.data.isProcessing) return;
    this.setData({ isProcessing: true, disabled: true });
    setTimeout(() => {
      this.setData({
        disabled: false
      })
    }, 2000);
    let _this = this
    wx.login({
      success(res) {
        if (res.code) {
          //这里下单
          let param = _this.data.param
          param.receiverName = _this.data.formData.contactName
          param.receiverTel = _this.data.formData.phoneNum
          param.loginCode = res.code
          app.order.placeOrder(param).then(res => {
            if (res.code === 200) {
              console.log(res)
              wx.setStorageSync('cart', [])

              _this.setData({
                disabled: false,
                totalCount: 0,
                totalPrice: 0,
                isProcessing: false
              })
              let orderSn = res.data.orderSn
              wx.requestPayment({
                timeStamp: res.data.timeStamp,
                nonceStr: res.data.nonceStr,
                package: res.data.package,
                signType: 'MD5',
                paySign: res.data.paySign,
                success(res) {
                  wx.navigateTo({
                    url: '../tasks/details/details?orderno=' + orderSn,
                  })
                },
                fail(err) {
                  wx.navigateTo({
                    url: '../tasks/details/details?orderno=' + orderSn,
                  })
                }
              })



            }
          })
        }
      }
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    const data = JSON.parse(decodeURIComponent(options.data))
    this.setData({
      param: data,
      'navigationSetting.deliveryPointName' :data.deliveryPointName
    })
    console.log(data)
  },
  goDetail: function (e) {
    console.log(e.detail, 'e.detail')
    wx.navigateTo({
      url: '../tasks/details/details?taskId=' + e.currentTarget.dataset.word
    })
  },
  confirmIsShowModal(e) {
    wx.showTabBar()
    this.setData({
      isShowModal: false
    })
    this.setData({
      isShowOpacity: false
    })
    wx.setPageStyle({
      style: {
        overflow: ''
      }
    })
  },
  confirmIsShowOpacity(e) {
    this.setData({
      isShowOpacity: e.detail
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
    console.log('onReady')
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    let _this = this
    app.order.getPayPageInfo().then(res => {
      if (res.code === 200) {
        _this.setData({
          'formData.contactName': res.data.receiveName,
          'formData.phoneNum': res.data.receivePhone,
          'formData.newReceivePhone': res.data.newReceivePhone
        })
        console.log(_this.data.formData.contactName)
      }
    })
  },
  editAddress: function () {
    wx.navigateTo({
      url: '/pages/fillInfo/fillInfo?receiveName=' + this.data.formData.contactName + '&receivePhone=' + this.data.formData.phoneNum,
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
  }
})